<template>
  <div class="list-wrap">
    <div class="list">
      <p class="tit">
        <span class="space"></span>
        <span class="No">序号</span>
        <span class="text-left">专利名称</span>
        <span class="text-left">申请号</span>
        <span class="text-left">公开/公告号</span>
        <span class="text-left sp1">申请日</span>
        <span class="sp1">公开/公告日</span>
        <span class="">申请人</span>
        <span class="">发明人</span>
        <span class="sp1">法律状态</span>
      </p>
      <ul class="context">
        <el-checkbox-group v-model="checkBoxList">
          <li v-for="(item, index) in this.listData" :key="index">
            <p class="space">
              <el-checkbox
                class="list-checkbox"
                :label="item"
                >{{
              }}</el-checkbox>
            </p>
            <p class="No">{{ index + 1 }}</p>
            <p class="ttl text-left">
              <span
                v-show="defaultTrans === false"
                :title="origListData[index].ttl"
                @click="toDetail(index)"
                v-html="item.ttl"
              ></span>
              <span
                v-show="defaultTrans === true"
                :title="origListData[index].ttl_zh"
                @click="toDetail(index)"
                v-html="item.ttl_zh"
              ></span>
            </p>
            <p
              class="text-left"
              :title="origListData[index].apn"
              v-html="item.apn"
            ></p>
            <p class="pn-info text-left">
              <span
                :title="origListData[index].pn"
                v-html="item.pn"
                style="cursor: text"
              ></span>
            </p>
            <p
              class="text-left sp1"
              :title="origListData[index].apd"
              v-html="item.apd"
            ></p>
            <p class="sp1" :title="origListData[index].isd" v-html="item.isd"></p>
            <p class="text-left" v-html="item.an"></p>
            <p class="text-left" v-html="item.in"></p>
            <p class="sp1">
              <span
                class="status"
                :class="{
                  published: item.lss === '公开',
                  valid: item.lss === '授权',
                }"
                v-html="item.lss"
              ></span>
            </p>
          </li>
        </el-checkbox-group>
      </ul>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText" />
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { disassembleWord, wordsLight, isZh } from "@/utils/utils.js";
export default {
  props: {
    data: {
      type: Object,
    },
    emptyText: {
      type: String,
    },
    checkAllVal: {
      type: Boolean,
    },
  },
  data() {
    return {
      listData: [],
      origListData: [],
      checkBoxList: [],
      noData: false,
    };
  },
  computed: {
    ...mapState(["searchWord", "highlightModeSettings", "defaultTrans"]),
  },
  methods: {
    getListData(data, option = { needCollection: true }) {
      let result = data.result || [];
      let list = [];
      let origList = [];
      this.noData = false;
      result.forEach((v, index) => {
        let obj = {
          id: v.pn,
          pn: v.pn || "--", // 公开/公告号
          ttl: v.ttl || v.ttl_zh || v.ttl_en || "暂无", // 标题
          ttl_zh: v.ttl_zh || v.ttl || "", // 中文标题
          // ttl_en: v.ttl_en || "", // 英文标题
          apn: v.apn || "--", // 申请号
          apd: v.apd || "--", // 申请日
          isd: v.isd || "--", // 公开/公告日
          ipc: (v.ipc && v.ipc.join("、")) || "--", // ipc
          an: (v.an || []).join("、") || "--", // 申请人
          in: (v.in || []).join("、") || "--", // 发明人
          lss: v.leg_det || "--", // 法律状态
          // showTitTrans: this.defaultTrans, // 标题翻译
          isCollection: false, // 是否收藏，默认为false
        };
        origList.push(obj);
      });

      // 翻译开关
      // if (this.defaultTrans) {
      //   origList.forEach((v, i) => {
      //     v.showTitTrans = false;
      //     // this.transalte(v, i);
      //   });
      // } else {
      //   origList.forEach((v, i) => {
      //     v.showTitTrans = true;
      //     // this.transalte(v, i);
      //   });
      // }

      this.origListData = origList;

      list = JSON.parse(JSON.stringify(origList)); // 高亮模式下的数据

      // 是否收藏
      // if (list.length > 0 && option.needCollection) {
      //   let pns = [];
      //   list.forEach((v) => {
      //     pns.push(v.pn);
      //   });

      //   this.isCollection(pns, (data) => {
      //     data.forEach((v) => {
      //       let index = origList.findIndex((b) => b.pn === v);
      //       this.$set(list[index], "isCollection", true);
      //     });
      //   });
      // }

      // 高亮模式
      let settings = [];
      let open = this.highlightModeSettings.open;
      // 默认关键词高亮样式
      let wordArr = disassembleWord(this.searchWord);
      // let defObj = wordArr;
      let defObj = [];
      if (open) {
        // 如果开启高亮模式
        settings = [...defObj, ...this.highlightModeSettings.modes];
      } else {
        settings = defObj;
      }
      list.forEach((v) => {
        v.ttl = wordsLight(v.ttl, settings);
        v.ttl_zh = wordsLight(v.ttl_zh, settings);
        v.apn = wordsLight(v.apn, settings);
        v.apd = wordsLight(v.apd, settings);
        v.pn = wordsLight(v.pn, settings);
        v.isd = wordsLight(v.isd, settings);
        v.ipc = wordsLight(v.ipc, settings);
        v.lss = wordsLight(v.lss, settings);
      });

      this.listData = list;

      this.noData = origList.length <= 0 ? true : false;
    },

    // 翻译
    // transalte(item, index) {
    //   let ttl = item.ttl;
    //   if (item.showTitTrans === true) {
    //     item.showTitTrans = false;
    //   } else {
    //     // 标题
    //     if (item.transTtl && item.transTtl !== "") {
    //       item.showTitTrans = true;
    //     } else {
    //       // 判断是否为中文
    //       let ttlType = "";
    //       if (isZh(item.ttl)) {
    //         ttlType = "ttl_en";
    //       } else {
    //         ttlType = "ttl_zh";
    //       }
    //       if (item[ttlType] && item[ttlType] !== "") {
    //         this.$set(item, "transTtl", item[ttlType]);
    //         item.showTitTrans = true;
    //       } else {
    //         if (this.origListData[index] && this.origListData[index].pn) {
    //           this.transField(ttl, this.origListData[index].pn, (data) => {
    //             this.$set(item, "transTtl", data);
    //             item.showTitTrans = true;
    //           });
    //         }
    //       }
    //     }
    //   }
    // },
    // 对应具体的翻译字段
    transField(str, pn, func) {
      // return
      // 过滤一些特殊字符，否则无法正常翻译
      let newStr = str.replace(/&nbsp;/g, "").replace(/<br>/g, "\n"); // .replace(/[\n]/g, ' ')
      this.$post(this.$Url.transalte, {
        text: newStr,
        pn,
      })
        .then((res) => {
          let translatedObj =
            res.data &&
            res.data.translation &&
            res.data.translation[0] &&
            res.data.translation[0].translated[0];
          let translatedText = translatedObj.text;
          translatedText = translatedText.replace(/\n/g, "<br/>");
          func && func(translatedText);
        })
        .catch((err) => {});
    },
    // 跳转详情
    toDetail(index) {
      let detailLeftMenu = [];
      this.origListData.forEach((v) => {
        detailLeftMenu.push({
          id: v.pn || "--", // 申请号
          title: v.ttl, // 标题
        });
      });
      localStorage.setItem("detailLeftMenu", JSON.stringify(detailLeftMenu));

      this.$nextTick(() => {
        let id = this.origListData[index].pn;
        let routerUrl = this.$router.resolve({
          name: "DataCenterDetail",
          query: {
            id,
            type: "patent",
          },
        });
        window.open(routerUrl.href, "_blank");
      });
    },
    // 展开详情
    showDetail(index) {
      let pn = this.origListData[index].pn;
      this.$emit("currentPn", pn);
    },
    // 是否收藏
    // isCollection(pns, func) {
    //   this.$post(this.$Url.account.isCollection, pns)
    //     .then((res) => {
    //       let data = res.data;
    //       func && func(data);
    //     })
    //     .catch((err) => {});
    // },
    // 收藏
    // collection(index) {
    //   let pn = this.origListData[index].pn; // 从源数据中获取pn
    //   let title = this.origListData[index].ttl[0]; // 从源数据中获取title
    //   if (this.listData[index].isCollection) {
    //     // 取消收藏
    //     this.$get(this.$Url.account.cancelCollection, {
    //       pn,
    //     })
    //       .then((res) => {
    //         this.$set(this.listData[index], "isCollection", false);
    //         this.$message({
    //           type: "success",
    //           duration: 1000,
    //           message: "取消收藏成功！",
    //         });
    //       })
    //       .catch((err) => {});
    //   } else {
    //     // 收藏
    //     this.$post(this.$Url.account.addCollection, {
    //       pn,
    //       title,
    //     })
    //       .then((res) => {
    //         let data = res.data;
    //         this.$set(this.listData[index], "isCollection", data);
    //         this.$message({
    //           type: "success",
    //           duration: 1000,
    //           message: "收藏成功！",
    //         });
    //       })
    //       .catch((err) => {});
    //   }
    // },
  },
  watch: {
    data() {
      this.getListData(this.data);
    },
    defaultTrans() {
      // if (this.defaultTrans === true) {
      //   this.listData.forEach((v, i) => {
      //     v.showTitTrans = false; // 标题翻译
      //     // this.transalte(v, i);
      //   });
      // } else {
      //   this.listData.forEach((v, i) => {
      //     v.showTitTrans = true; // 标题翻译
      //     // this.transalte(v, i);
      //   });
      // }
    },
    // 多选
    checkBoxList() {
      this.$Bus.$emit("getDcCheckBoxlist", this.checkBoxList);
    },
    // 全选
    checkAllVal() {
      this.checkBoxList = this.checkAllVal ? this.listData : [];
    },
    // 高亮模式
    highlightModeSettings: {
      deep: true,
      handler: function () {
        this.getListData(this.data, { needCollection: false });
      },
    },
  },
  mounted() {
    this.getListData(this.data);
  },
};
</script>

<style lang="scss" scoped>
.list-wrap {
  position: relative;
  .list {
    position: relative;
    width: 100%;
    .tit {
      position: relative;
      display: flex;
      width: 100%;
      height: 41px;
      line-height: 41px;
      margin-top: 0;
      margin-bottom: 0;
      font-size: 14px;
      font-weight: bold;
      color: $color10;
      background-color: $color154;
      span {
        flex: 1;
        margin-right: 10px;
        text-align: center;
        font-size: 12px;
        &.space {
          flex: none;
          width: 35px;
        }
        &.No {
          flex: none;
          width: 40px;
          margin-right: 10px;
        }
      }
      .text-left {
        text-align: left;
      }
    }
    .context {
      position: relative;
      width: 100%;
      margin-top: 0;
      margin-bottom: 0;
      list-style: none;
      padding-left: 0;
      li {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        min-height: 60px;
        border-bottom: 1px solid $color2;
        box-sizing: border-box;
        background-color: $color2 !important;
        // &:nth-of-type(2n-1) {
        //   background-color: $color4;
        // }
        &.no-data {
          background-color: #fff;
        }
        p {
          position: relative;
          flex: 1;
          line-height: 1.5;
          padding: 12px 0;
          margin-top: 0;
          margin-bottom: 0;
          margin-right: 10px;
          text-align: center;
          font-size: 12px;
          font-weight: 700;
          color: $color150;
          word-break: break-all;
          word-wrap: break-word;
          > span {
            font-size: 12px;
          }
          &:nth-of-type(1) {
            text-align: left;
          }
          &.space {
            flex: none;
            width: 35px;
            text-align: right;
            .list-checkbox {
              position: absolute;
              top: 2px;
              right: -7px;
              padding-left: 12px;
            }
          }
          &.No {
            flex: none;
            width: 40px;
            margin-right: 10px;
            color: $color6;
          }
          &.ttl {
            text-align: left;
            > span {
              cursor: pointer;
            }
          }
          &.ipc {
            text-align: left;
          }
          &.text-left {
            text-align: left;
          }
          .status {
            display: inline-block;
            min-width: 44px;
            line-height: 26px;
            border-radius: 3px;
            box-sizing: border-box;
            padding: 0 5px;
            margin: 0 5px;
            font-size: 12px;
            font-weight: normal;
            color: $color2;
            background-color: $mainColor;
          }
        }
      }
    }
  }
}

.pn-info {
  position: relative;
  span {
    display: inline-block;
    min-height: 30px;
    line-height: 30px;
    margin-right: 5px;
    vertical-align: middle;
    cursor: pointer;
    &.icon {
      display: inline-block;
      width: 24px;
      margin-top: -2px;
      margin-right: 5px;
      text-align: center;
      font-weight: normal;
      font-size: 16px !important;
      cursor: pointer;
    }
    &.iconzhankai2 {
      margin-top: 0;
      font-size: 20px !important;
    }
    &.iconshoucang11 {
    }
    &.iconshoucang3 {
      color: $mainColor;
    }
  }
}
.sp1 {
  flex: none !important;
  width: 80px;
}
// .sp2 {
//   flex: none !important;
//   width: 14%;
//   min-width: 160px;
// }
// .sp3 {
//   flex: none !important;
//   width: 10%;
//   max-width: 140px;
// }
.no-data {
  position: relative;
  line-height: 50px;
  text-align: center;
  font-size: 14px;
  color: $color6;
  background-color: $color2;
}
</style>